import { ResPage } from "@/api/interface";
import { Blog, Subfield } from "@/api/interface/blog";
import { BlogState } from "@/redux/interface";
import * as types from "@/redux/mutation-types";
import produce from "immer";

export type BlogAction =
	| {
			type: "SET_BLOG_LIST";
			payload: ResPage<Blog>;
	  }
	| {
			type: "SET_BLOG_DETAIL";
			payload: Blog;
	  }
	| {
			type: "SET_SUBFIELD_LIST";
			payload: Subfield[];
	  };

const blogState: BlogState = {
	blogList: [],
	blogDetail: {} as any,
	blogTotal: 0,
	subfieldList: []
};

export default function blog(state = blogState, action: BlogAction) {
	return produce(state, draftState => {
		switch (action.type) {
			case types.SET_BLOG_LIST:
				draftState.blogList = action.payload.rows;
				draftState.blogTotal = action.payload.total;
				break;
			case types.SET_BLOG_DETAIL:
				draftState.blogDetail = action.payload;
				break;
			case types.SET_SUBFIELD_LIST:
				draftState.subfieldList = action.payload;
				break;
			default:
				break;
		}
	});
}
